package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGiftReciveResponseParams;
import com.foolishbird.sdk.params.FloatGiftResponseParams;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.FloatNoticeResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGift;
import com.foolishbird.sdk.params.bean.FloatNoticeInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public class FloatGiftFragment extends BaseHttpFragment implements View.OnClickListener {


    private ListView mListview;
    private TextView titleTxt ,giftNewTxt,getMyTxt;
    private ImageView backView;
    private View giftNewLine,giftMyLine;

    public static final FloatGiftFragment newInstance(Bundle bundle) {
        FloatGiftFragment fragment = new FloatGiftFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private boolean currentIsNew = true;
    private MyAdapter myAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_gift");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("游戏礼包");

        giftNewTxt = contentView.findViewById(getViewIdentifier("float_gift_new_txt"));
        giftNewTxt.setOnClickListener(this);
        getMyTxt = contentView.findViewById(getViewIdentifier("float_gift_my_txt"));
        getMyTxt.setOnClickListener(this);
        giftNewLine = contentView.findViewById(getViewIdentifier("float_gift_new_line"));
        giftMyLine = contentView.findViewById(getViewIdentifier("float_gift_my_line"));

        mListview = contentView.findViewById(getViewIdentifier("float_notice_list"));
        myAdapter = new MyAdapter();
        mListview.setAdapter(myAdapter);
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

//                Bundle bundle = new Bundle();
//                bundle.putSerializable("notice",myNoticeList.get(i));
//                bundle.putString("title","攻略详情");
//                MyFragmentManger.getInstance().gotoFragment(FloatNoticeInfoFragment.newInstance(bundle));
            }
        });

        HttpEngineApi.getInstance().getFloatGift(requestCallbackHandler(FloatGiftResponseParams.class));
        return contentView;

    }

    private List<FloatGift> newGiftList,recivedGiftList;


    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof FloatGiftResponseParams){
                    List<FloatGift> myNoticeList =((FloatGiftResponseParams)response).getData().getCardData();
                    if(myNoticeList==null||myNoticeList.size()==0)return;
                    newGiftList = new ArrayList<>();
                    recivedGiftList = new ArrayList<>();
                    for (FloatGift gfit:myNoticeList ) {
                        newGiftList.add(gfit);
                        if(1!=gfit.getIsReceived()){
                            recivedGiftList.add(gfit);
                        }
                    }
                   myAdapter.setData(newGiftList);
                    myAdapter.notifyDataSetChanged();
                }else if (response instanceof  FloatGiftReciveResponseParams){
                    FloatGift myNoticeList =((FloatGiftReciveResponseParams)response).getData().getCardData();
                    int id = (int)params[0];
                    int index = 0;
                    for (int i =0;i<newGiftList.size();i++){
                        FloatGift temp = newGiftList.get(i);
                        if(temp.getId() == id){
                            temp.setCardNo(myNoticeList.getCardNo());
                            temp.setIsReceived(2);
                            recivedGiftList.add(temp);
                            index = i;
                            break;
                        }
                    }
//                    newGiftList.remove(index);
                    myAdapter.setData(newGiftList);
                    myAdapter.notifyDataSetChanged();
                    Bundle bundle =new Bundle();
                    bundle.putSerializable("gift",newGiftList.get(index));
                    MyFragmentManger.getInstance().gotoFragment(FloatGiftSuccessFragment.newInstance(bundle));
                }
            }
            setAdapter();
        }



        WL.d("FloatNoticeFragment",response.toString());
    }


   class  MyAdapter extends BaseAdapter{

        private List<FloatGift> list;

        public void setData(List<FloatGift> newGiftList){
            list = newGiftList;
        }

       @Override
       public int getCount() {
           return list==null?0:list.size();
       }

       @Override
       public Object getItem(int i) {
           return list.get(i);
       }

       @Override
       public long getItemId(int i) {
           return i;
       }

       @Override
       public View getView(int i, View view, ViewGroup viewGroup) {
           ViewHolder holder = null;
           if (view == null) {
               int resource = ResUtils.getLayoutIdentifier(getContext(), "fb_float_gift_item");
               view = LayoutInflater.from(getContext()).inflate(resource, null);
               holder = new ViewHolder();
               holder.titleText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_notice_title"));
               holder.numberText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_gift_number"));
               holder.contenText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_gift_des"));
               holder.btnText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "float_gift_btn"));
               view.setTag(holder);
           } else {
               holder = (ViewHolder) view.getTag();
           }
           FloatGift menu =list.get(i);
           holder.titleText.setText(menu.getName());
           holder.numberText.setText(menu.getTotalInUse()+"人");
           holder.contenText.setText(menu.getContent());
           if(menu.getIsReceived()==1){
               holder.btnText.setText("待领取");
           }else{
               holder.btnText.setText("复制");
           }
           holder.btnText.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   if (menu.getIsReceived()==1){
                       taskId = HttpEngineApi.getInstance().floatGiftRecive(menu.getId()+"", requestCallbackHandler(FloatGiftReciveResponseParams.class,menu.getId()));
                   }else {
                       //获取剪贴板管理器：
                       ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

                       String temp = (String)menu.getCardNo();

                       // 创建普通字符型ClipData
                       ClipData mClipData = ClipData.newPlainText("Label", temp);
                       // 将ClipData内容放到系统剪贴板里。
                       cm.setPrimaryClip(mClipData);
                       Toast.makeText(getContext(),"复制成功",Toast.LENGTH_SHORT).show();
                   }
               }
           });
           return view;
       }
   }

    private void setAdapter(){

    }

    class ViewHolder {
        TextView titleText;
        TextView numberText;
        TextView contenText;
        TextView btnText;
    }

    private int taskId;

    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }else if(view ==giftNewTxt){
            if(!currentIsNew){
                currentIsNew = true;
                myAdapter.setData(newGiftList);
                myAdapter.notifyDataSetChanged();
                giftNewTxt.setTextColor(Color.parseColor("#19b60e"));
                giftNewLine.setBackgroundColor(Color.parseColor("#19b60e"));
                getMyTxt.setTextColor(Color.parseColor("#d1d1d1"));
                giftMyLine.setBackgroundColor(Color.parseColor("#ffffff"));
            }
        }else if(view ==getMyTxt){
            if(currentIsNew){
                currentIsNew = false;
                myAdapter.setData(recivedGiftList);
                myAdapter.notifyDataSetChanged();
                giftNewTxt.setTextColor(Color.parseColor("#d1d1d1"));
                giftNewLine.setBackgroundColor(Color.parseColor("#ffffff"));
                getMyTxt.setTextColor(Color.parseColor("#19b60e"));
                giftMyLine.setBackgroundColor(Color.parseColor("#19b60e"));
            }
        }

    }

}
